package com.sz.web.sysmanager.control;


import com.sz.web.account.entity.AccountFunction;
import com.sz.web.account.entity.AccountRole;
import com.sz.web.account.enums.FunctionStatus;
import com.sz.web.account.mapper.AccountFunctionMapper;
import com.sz.web.account.mapper.AccountRoleMapper;
import com.sz.web.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *@Description 系统管理控制层
 *@Author lensener
 */
@Controller
@RequestMapping(value = "/sysmanager")
public class SysManagerPageController {
    @Resource
    private AccountRoleMapper accountRoleMapper;
    @Resource
    private AccountFunctionMapper accountFunctionMapper;

    @RequestMapping(value = "/syslog")
    public String sysmanager() {
        return "sysmanager/syslog";
}

    @RequestMapping(value = "/person_manager")
    public String sysmanagerPersonManager() {
        return "sysmanager/person_manager";
    }

    @RequestMapping(value = "/department_manager")
    public String sysmanagerDepartmentManager() {
        return "sysmanager/department_manager";
    }

    @RequestMapping(value = "/role_manager")
    public String sysmanagerRoleManager() {
        return "sysmanager/role_manager";
    }

    @RequestMapping(value = "/architecture_manager")
    public String sysmanagerArchitectureManager() {
        return "sysmanager/architecture_manager";
    }

    @RequestMapping(value = "/role_list")
    public String sysmanageRoleList(Model model) {
        List<AccountRole> accountRoles = accountRoleMapper.selectAll();
        model.addAttribute("accountRoles", accountRoles);
        return "sysmanager/role_list";
    }

    @RequestMapping(value = "/role_add")
    public String sysmanageRoleAdd() {
        return "sysmanager/role_add";
    }

    @RequestMapping(value = "/function_list")
    public String sysmanageFunctionList(Integer roleId, Model model) {

        //获取所有功能
        List<AccountFunction> accountFunctions = accountFunctionMapper.selectAll();
        //根据角色id获取功能
        List<AccountFunction> accountFunctionsByRole = accountFunctionMapper.selectByRoleId(roleId);

        //设置功能状态字段
        for (AccountFunction accountFunction:accountFunctions){
            for (AccountFunction accountFunctionByRole: accountFunctionsByRole){
                if (accountFunction.getId().equals(accountFunctionByRole.getId())){
                    accountFunction.setStatus(FunctionStatus.USING.getStatus());
                }
            }
        }

        List<List<AccountFunction>> functions = groupFunctionByUrl(accountFunctions);
        model.addAttribute("accountFunctionsByRole", accountFunctionsByRole);
        model.addAttribute("functions", functions);
        return "sysmanager/function_list";
    }

    /**
     * 将权限对象按第一截路由分组
     * @param accountFunctions
     * @return
     */
    private static List<List<AccountFunction>> groupFunctionByUrl( List<AccountFunction> accountFunctions){
        //按第一截路由分组，例：/user/register  /sysmanager/syslog 为两组
        List<List<AccountFunction>> groupFunctionList = new ArrayList<>();

        List<String> subUrls = new ArrayList<>();
        //外层遍历
        for (AccountFunction accountFunction : accountFunctions) {
            List<AccountFunction> groupFunctionEle = new ArrayList<>();

            String controlUrl = accountFunction.getControlUrl();
            int endSubIndexOut = StringUtils.getFromIndex(controlUrl, "/", 2);
            if (endSubIndexOut == -1){
                continue;
            }
            String firstSubUrl = controlUrl.substring(0, endSubIndexOut);

            if (subUrls.contains(firstSubUrl)){
                continue;
            }else {

                subUrls.add(firstSubUrl);
                //里层遍历
                for (AccountFunction accountFunctionIn : accountFunctions){
                    if (accountFunctionIn.getStatus().equals(FunctionStatus.FORBIDDEN.getStatus())){
                        continue;
                    }
                    String controlUrlIn = accountFunctionIn.getControlUrl();
                    int endSubIndexIn = StringUtils.getFromIndex(controlUrlIn, "/", 2);
                    if (endSubIndexIn == -1){
                        continue;
                    }
                    String firstSubUrlIn = controlUrlIn.substring(0, endSubIndexIn);
                    if (firstSubUrl.equals(firstSubUrlIn)){
                        groupFunctionEle.add(accountFunctionIn);
                    }
                }

            }
            groupFunctionList.add(groupFunctionEle);
        }
        return groupFunctionList;
    }
}
